package com.whut.monitor.service.impl;

import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.Tree;
import com.whut.monitor.constant.PositionType;
import com.whut.monitor.dao.IBaseDao;
import com.whut.monitor.model.ChannelEntity;
import com.whut.monitor.model.EquipmentEntity;
import com.whut.monitor.model.SensorEntity;
import com.whut.monitor.service.IChannelService;
import com.whut.monitor.vo.ChannelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Sandeepin
 * 通道服务层实现
 */
@Service
public class ChannelServiceImpl implements IChannelService {

	@Autowired
	private IBaseDao<ChannelEntity> channelDao;
	@Autowired
	private IBaseDao<SensorEntity> sensorDao;
	
	@Override
	public void add(ChannelVo channelVo) {
		ChannelEntity entity = new ChannelEntity();
		entity.setChannelNum(channelVo.getChannelNum());

		entity.setChannelName(channelVo.getChannelName());
		entity.setPositionType(channelVo.getPositionType());
		entity.setPositionNum(channelVo.getPositionNum());
		entity.setCreateTime(channelVo.getCreateTime());
		entity.setDescrip(channelVo.getDescrip());

		EquipmentEntity equipt=new EquipmentEntity();
		equipt.setId(channelVo.getEquipmentId());
		entity.setEquipmentEntity(equipt);

		SensorEntity senr=new SensorEntity();
		senr.setId(channelVo.getSensorId());
		entity.setSensorEntity(senr);

		channelDao.save(entity);
	}
	

	@Override
	public void delete(Integer id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		channelDao.executeHql("delete from ChannelEntity entity where entity.id=:id",params);
	}

	
	@Override
	public void edit(ChannelVo channelVo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("channelNum", channelVo.getChannelNum());
		params.put("channelName", channelVo.getChannelName());
		params.put("positionType", channelVo.getPositionType());
		params.put("positionNum", channelVo.getPositionNum());
		params.put("createTime", channelVo.getCreateTime());
		params.put("descrip", channelVo.getDescrip());
		EquipmentEntity equipment=new EquipmentEntity();
		equipment.setId(channelVo.getEquipmentId());
		params.put("equipment", equipment);

		SensorEntity sensor = new SensorEntity();
		sensor.setId(channelVo.getSensorId());
		params.put("sensor", sensor);

		params.put("id", channelVo.getId());
		String hql="update ChannelEntity entity set entity.channelNum=:channelNum,entity.channelName=:channelName,entity.positionType=:positionType"
				+ ",entity.positionNum=:positionNum,entity.createTime=:createTime,entity.descrip=:descrip,entity.equipmentEntity=:equipment,entity.sensorEntity=:sensor where entity.id=:id";
		channelDao.executeHql(hql, params);
	}

	
	@Override
	public List<ChannelVo> dataGrid(ChannelVo channelVo, PageFilter pageFilter){
		List<ChannelVo> ul = new ArrayList<ChannelVo>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from ChannelEntity entity ";
		List<ChannelEntity> list = channelDao.findList(hql + whereHql(channelVo, params) + orderHql(pageFilter), params, pageFilter.getPage(), pageFilter.getRows());
		for (ChannelEntity entity : list) {
			ChannelVo u = new ChannelVo();
			BeanUtils.copyProperties(entity, u);
			
			u.setEquipmentId(entity.getEquipmentEntity().getId());
			u.setEquipmentName(entity.getEquipmentEntity().getEquipmentName());

			u.setSensorId(entity.getSensorEntity().getId());
			u.setSensorName(entity.getSensorEntity().getSensorName());
			
			if(PositionType.AXIS.getType()==entity.getPositionType()){
				u.setPositionTypeName(PositionType.AXIS.getName());
			}else if (PositionType.RAIL.getType()==entity.getPositionType()) {
				u.setPositionTypeName(PositionType.RAIL.getName());
			}else if (PositionType.PILLAR.getType()==entity.getPositionType()) {
				u.setPositionTypeName(PositionType.PILLAR.getName());
			}
			ul.add(u);
		}
		return ul;
	}
	
	
	@Override
	public Long count(ChannelVo channelVo, PageFilter pageFilter) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from ChannelEntity entity ";
		return channelDao.count("select count(*) " + hql + whereHql(channelVo, params), params);
	}
	
	private String whereHql(ChannelVo channelVo, Map<String, Object> params) {
		String hql = "";
		if (channelVo != null) {
			hql += " where 1=1 ";
			if (channelVo.getChannelName() != null) {
				hql += " and entity.name like :name";
				params.put("name", "%%" + channelVo.getChannelName() + "%%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter pageFilter) {
		String orderString = "";
		if ((pageFilter.getSort() != null) && (pageFilter.getOrder() != null)) {
			orderString = " order by entity." + pageFilter.getSort() + " " + pageFilter.getOrder();
		}
		return orderString;
	}

	@Override
	public List<Tree> tree() {
		List<ChannelEntity> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		l = channelDao.findList("select distinct entity from ChannelEntity entity");

		if ((l != null) && (l.size() > 0)) {
			for (ChannelEntity r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());

				lt.add(tree);
			}
		}
		return lt;
	}

	@Override
	public List<ChannelEntity> getChannelByEquip(Integer equipmentId,Integer channelType) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("equipmentId", equipmentId);
		String hql="from ChannelEntity c where c.equipmentEntity.id=:equipmentId";
		if (channelType!=null) {
			params.put("sensorId", channelType);
			hql=hql+" and c.sensorEntity.id=:sensorId";
		}
		List<ChannelEntity> list=channelDao.findList(hql,params);
		return list;
	}

	@Override
	public List<ChannelEntity> getSensorTypeByEquip(Integer equipmentId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("equipmentId", equipmentId);
		String hql="from ChannelEntity c where c.id in (select min(c.id) from ChannelEntity c where c.equipmentEntity.id=:equipmentId group by c.sensorEntity.id)";
		List<ChannelEntity> list=channelDao.findList(hql,params);
		return list;
	}

}
